package com.udaos.modules.trainer.services;

import java.util.ArrayList;
import java.util.List;

import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import com.udaos.common.base.Service;
import com.udaos.models.trainer.Ud_trainer;
import com.udaos.models.trainer.Ud_trainer_project;

/**
 * @author mutaotao
 * @time 2016-12-15 10:36:44
 * 
 */
@IocBean(args = { "refer:dao" })
public class UdTrainerService extends Service<Ud_trainer> {
	
	@Inject
	private UdTrainerProjectService trainerProjectService;

	public UdTrainerService(Dao dao) {
		super(dao);
	}

	/**
	 * 新增教练
	 * 
	 * @param ud_coach
	 * @param fieldid
	 */
	@Aop(TransAop.READ_COMMITTED)
	public void save(Ud_trainer trainer, String projects) {
		dao().insert(trainer);
		//设置其项目
		if(!Strings.isBlank(projects)){
			List<Ud_trainer_project> lists=new ArrayList<Ud_trainer_project>();
			if (projects.startsWith("[")) {
				projects = projects.substring(1, projects.length() - 1);
				projects=projects.replace("\"", "");
				String [] s=projects.split(",");
				for(String ss:s){
					Ud_trainer_project trainer_project=new Ud_trainer_project();
					trainer_project.setCreateAt((int)(System.currentTimeMillis()/1000));
					trainer_project.setProject_id(ss.trim());
					trainer_project.setTrainer_id(trainer.getId());
					lists.add(trainer_project);
				}
			}else{
				Ud_trainer_project trainer_project=new Ud_trainer_project();
				trainer_project.setCreateAt((int)(System.currentTimeMillis()/1000));
				trainer_project.setProject_id(projects);
				trainer_project.setTrainer_id(trainer.getId());
				lists.add(trainer_project);
			}
			trainerProjectService.save(lists);
		}
	}


	/**
	 * 修改
	 * 
	 * @param trainer
	 * @param projects
	 */
	@Aop(TransAop.READ_COMMITTED)
	public void updateTrainer(Ud_trainer trainer,String projects) {
		dao().updateIgnoreNull(trainer);
		//设置其项目
		if(!Strings.isBlank(projects)){
			List<Ud_trainer_project> lists=new ArrayList<Ud_trainer_project>();
			if (projects.startsWith("[")) {
				projects = projects.substring(1, projects.length() - 1);
				projects=projects.replace("\"", "");
				String [] s=projects.split(",");
				for(String ss:s){
					Ud_trainer_project trainer_project=new Ud_trainer_project();
					trainer_project.setCreateAt((int)(System.currentTimeMillis()/1000));
					trainer_project.setProject_id(ss.trim());
					trainer_project.setTrainer_id(trainer.getId());
					lists.add(trainer_project);
				}
			}else{
				String [] s=projects.split(",");
				for(String ss:s){
					Ud_trainer_project trainer_project=new Ud_trainer_project();
					trainer_project.setCreateAt((int)(System.currentTimeMillis()/1000));
					trainer_project.setProject_id(ss.trim());
					trainer_project.setTrainer_id(trainer.getId());
					lists.add(trainer_project);
				}
			}
			//获取原有的删除
			trainerProjectService.deleteOld(trainer.getId());
			trainerProjectService.save(lists);
		}
	}

	/**
	 * 根据ID获取其拥有的项目
	 * @param id
	 * @return
	 */
	public List<Ud_trainer_project> findTrainerProjects(String id) {
		return trainerProjectService.query(Cnd.where("trainer_id", "=", id));
	}

}